package com.tensquare.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.user.dao.PermissionDao;
import com.tensquare.user.dto.ReqPermission;
import com.tensquare.user.exception.RecordException;
import com.tensquare.user.pojo.TbPermission;
import com.tensquare.user.service.PermissionService;
import com.tensquare.user.vo.Permission;
import com.tensquare.util.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service("permissionService")
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public List<TbPermission> getPermissionsByUserId(String userId) {
        return permissionDao.selectByUserId(userId);
    }

    @Override
    public Permission findOne(String id) {
        Permission permission = null;
        TbPermission tbPermission = permissionDao.selectById(id);
        if (tbPermission != null) {
            permission = new Permission();
            BeanUtils.copyProperties(tbPermission, permission);
        }
        return permission;
    }

    @Override
    public List<Permission> findByPage(int offset, int limit, int isParent) {
        List<Permission> permissions = new ArrayList<>();
        Page<TbPermission> tbPermissionPage = new Page<>(offset, limit);
        QueryWrapper<TbPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", isParent);
        permissionDao.selectPage(tbPermissionPage, wrapper);
        for (TbPermission record : tbPermissionPage.getRecords()) {
            Permission permission = new Permission();
            BeanUtils.copyProperties(record, permission);
            permissions.add(permission);
        }
        return permissions;
    }

    @Override
    public void delPer(ArrayList<Integer> ids) {
        List<TbPermission> permissions = findList(ids);
        if (permissions.size() == 0) {
            throw new RecordException("未找到记录");
        }
        //删除关联的子权限
        for (TbPermission permission : permissions) {
            if (permission.getParentId() == -1) {
                QueryWrapper<TbPermission> wrapper = new QueryWrapper<>();
                wrapper.eq("parent_id", permission.getId());
                permissionDao.delete(wrapper);
            }
        }
        permissionDao.deleteBatchIds(ids);
        permissionDao.delRolePerByPermissionId(ids);
//        permissionDao.delRolePerByPermissionId()
//        int i = split.length - success;
//        String msg = i == 0 ? "删除成功" : "删除成功：" + permissions.size() + ",失败：" + i;
//        return new Result(true, msg, StatusCode.OK);
    }


    @Override
    public void delMainPer(String id) {
        QueryWrapper<TbPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        permissionDao.delete(wrapper);
        permissionDao.deleteById(id);
    }

    @Override
    public void updatePer(Map<String,Object> map) {
        UpdateWrapper<TbPermission> wrapper = new UpdateWrapper<>();
        Integer id = (Integer) map.get("id");
        map.remove("id");
        for (String s : map.keySet()) {
            wrapper.set(s,map.get(s));
        }
        wrapper.set("update_time",new Date());
        wrapper.eq("id",id);
        permissionDao.update(null,wrapper);
    }

    @Override
    public void addPer(ReqPermission reqPermission) {
        TbPermission tbPermission = new TbPermission();
        BeanUtils.copyProperties(reqPermission, tbPermission);
        tbPermission.setIsDelete(0);
        tbPermission.setIsDisable(0);
        Date date = new Date();
        tbPermission.setCreateTime(date);
        tbPermission.setUpdateTime(date);
        permissionDao.insert(tbPermission);
    }

    @Override
    public List<Permission> findByRoleId(Integer roleId) {
        List<TbPermission> tbPermissions = permissionDao.findByRoleId(roleId);
        ArrayList<Permission> permissions = new ArrayList<>();
        for (TbPermission tbPermission : tbPermissions) {
            Permission permission = new Permission();
            BeanUtils.copyProperties(tbPermission,permission);
            permissions.add(permission);
        }
        return permissions;
    }

    @Override
    public List<Permission> findAll() {
        ArrayList<Permission> permissions = new ArrayList<>();
        List<TbPermission> tbPermissions = permissionDao.selectList(null);
        for (TbPermission tbPermission : tbPermissions) {
            Permission permission = new Permission();
            BeanUtils.copyProperties(tbPermission,permission);
            permissions.add(permission);
        }
        return permissions;
    }

    @Override
    public List<Permission> findByParentId(Integer parentId) {
        ArrayList<Permission> permissions = new ArrayList<>();
        QueryWrapper<TbPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<TbPermission> tbPermissions = permissionDao.selectList(queryWrapper);
        for (TbPermission tbPermission : tbPermissions) {
            Permission permission = new Permission();
            BeanUtils.copyProperties(tbPermission,permission);
            permissions.add(permission);
        }
        return permissions;
    }

    public List<TbPermission> findList(ArrayList<Integer> ids) {
        return permissionDao.selectBatchIds(ids);
    }

}
